import sys
sys.path.append('..')

import pyglet
from pyglet.gl import *

from windoh import *
from windoh.renderers.opengl import OpenGLRenderer
#from window.renderers.cairo import CairoRenderer
from windoh.controls.label import Label
from windoh.controls.button import Button, TextButton
from windoh.controls.frame import Frame
from windoh.controls.window import Window 
from windoh.controls.slider import Slider
from windoh.controls.colorpicker import RGBSliders
from windoh.controls.dropdown import DropDown
from windoh.controls.textinput import TextInput
from windoh import layout

pyglet.resource.path.append('../data')
pyglet.resource.reindex()

window = pyglet.window.Window()

style = DefaultStyle({
    'Control': dict(
        display='block',
        cursor='default',
        font_size=10,
        background_style='solid',
    ),
    
    'Root': dict(
        background_color=(0.0, 0.3, 0.32),
    ),

    'Window': dict(
        border_color=(1.0, 1.0, 1.0), 
        border_style="outset", 
        border_width=1,
        min_width=100,
        min_height=100,
    ),
    '.window_title': dict(
        border_color=(1.0, 1.0, 1.0), 
        border_style="inset", 
        border_width=1,
        background_color2=(0.9, 0.4, 0.4),
        background_color=(0.9, 0.1, 0.1),
        background_style="gradient_horiz",
        cursor='size',
        text_align='center',
    ),
    '.window_client_area': dict(
        border_color=[1.0, 1.0, 1.0],
        border_style="inset",
        border_width=1,
        background_color=[0.7, 0.7, 0.7],
        overflow="clip",
    ),
    '.window_resize_handle': dict(
        #border_color=(0.0, 1.0, 1.0),
        #border_style="solid",
        #border_width=1,
        color=(0.5, 0.5, 0.5),
        padding=2,
    ),

    'Button': dict(
        border_color=(0.8, 0.8, 0.8), 
        border_style="outset", 
        border_width=2,
        background_color2=(0.7, 0.7, 0.7),
        background_color=(0.5, 0.5, 0.5),
        background_style="gradient_vert",
        color=(0.0, 0.0, 0.0),
        cursor="hand",
    ),

    'TextButtonLabel': dict(
        text_align="center",
    ),

    'Slider': dict(
        border_color=(0.8, 0.8, 0.8), 
        border_style="solid", 
        border_width=2,
        background_color=(0.4, 0.4, 0.4),
    ),
    'SliderHandle': dict(
        border_color=(0.8, 0.8, 0.8), 
        border_style="outset", 
        border_width=1,
        background_color=(0.7, 0.7, 0.7),
        cursor='size',
    ),

    'DropDown': dict(
        border_color=(0.8, 0.8, 0.8), 
        border_style="inset", 
        border_width=1,
        background_color=(0.9, 0.9, 0.9),
    ),   

    'DropDownList': dict(
        border_color=(0.0, 0.0, 0.0), 
        border_style="solid", 
        border_width=1,
        background_color=(0.9, 0.9, 0.9),
        overflow='clip',
    ),

    'DropDownListItem': dict(
        #border_color=(0.0, 0.0, 0.0), 
        #border_style="solid", 
        #border_width=1,
        background_color=(0.9, 0.9, 0.9),
    ),
    
    'DropDownListItem.hover': dict(
        background_color=(0.9, 0.3, 0.3),
    ),

    'DropDownLabel': dict(
        cursor="hand",
    ),

    'DropDownButton': dict(
        background_image=pyglet.resource.image('dropdown.png'),
        background_image_halign="center",
        background_image_valign="center",
        cursor="hand",
    ),

    'TextInput': dict(
        border_color=(0.8, 0.8, 0.8), 
        border_style="inset", 
        border_width=2,
        background_color=(0.9, 0.9, 0.9),
        cursor='text',
    ),

    'TextInputCaret': dict(
        background_color=(0.0, 0.0, 0.0),
    ),

    'RGBSliders': dict(
        border_color=(0.0, 0.0, 0.0), 
        border_style="solid", 
        border_width=1,

    ),
})
if True:
    renderer = OpenGLRenderer() #Renderer(Control=ControlRenderer())
else:
    renderer = CairoRenderer()

root = Root(window, renderer, style, )

control_a = Window("my frame",
    #border_color=(1.0, 0.5, 0.5),
    #border_style="solid",
    #border_width=2,
    #background_color=(0.3, 0.3, 0.3),
    left = 100,
    top = 200,
    width = 200,
    height = 200,
)
control_a._client_area.style.update(dict(
    layout = 'strict_grid',
    layout_columns = 2,
    #padding = 10,
))
root.append(control_a)

for i in range(5):
    control_b = TextButton(
        "some text",
        #border_color=(1.0, 1.0, 1.0),
        #border_style="solid",
        #border_width=1,
        right=10, bottom=40,
        width=100, top=20,
    )
    control_a.append(control_b)

    @control_b.event
    def on_click(*args):
        print "control_b", args

control_i = TextInput(
    "edut me",
    right=10, bottom=10,
    width=100, height=20,
)
#control_a.append(control_i)

control_c = Slider(
    left=10, top=10,
    width=20,
    bottom=10,
)
control_a.append(control_c)

@control_c.event
def on_value_change(v):
    style['Button']['background_color'] = (v/3, v/2, v)
        

control = Window("my frame",
    #border_color=(1.0, 0.5, 0.5),
    #border_style="solid",
    #border_width=2,
    #background_color=(0.3, 0.3, 0.3),
    left = 400,
    top = 200,
    width = 200,
    height = 200,
)
root.append(control)
color = style['.window_client_area']['background_color']
rgb_c = RGBSliders(color, left=10, right=10, top=10, bottom=10)
control.append(rgb_c)

@rgb_c.event
def on_value_change(v):
    style['.window_client_area']['background_color'] = v

control_d = Window(
    left = 100,
    top = 10,
    width = 100,
    height = 100,
)
control.append(control_d)

b = TextButton("random",
    top=10,
    height=20,
    left=80,
    width=80,
)
#control.append(b)
from random import random
@b.event
def on_click():
    rgb_c.color = [random(), random(), random()]

d = DropDown(['option 1', 'choose me', 'join usssssssss'], 
    top=40, 
    left=80, 
    height=20, 
    width=100,
)
#control.append(d)

if False:
    for x in range(100):
        control = Window("my frame",
            #border_color=(1.0, 0.5, 0.5),
            #border_style="solid",
            #border_width=2,
            #background_color=(0.3, 0.3, 0.3),
            left = 0,
            top = 200,
            width = 200,
            height = 200,
        )
        root.append(control)

control = Control(
    border_color=(1.0, 1.0, 1.0),
    border_style="outset",
    border_width=2,
    background_color=(0.3, 0.3, 0.3),
    left=100, top=100,
    cursor="hand",
)
root.append(control)

control = Control(
    border_color=(1.0, 0.5, 0.5),
    border_style="inset",
    border_width=2,
    background_color=(0.3, 0.3, 0.3),
    left = 100,
    top = 10,
    name="bob",
)
root.append(control)


cbox = Control(
    border_color=(1.0, 0.5, 0.5),
    border_style="solid",
    border_width=2,
    background_color=(0.3, 0.3, 0.3),
    left = 100,
    bottom = 10,
    width = 200,
    height = 200,
)
cbox.style['layout'] = layout.VertBox(cbox)
root.append(cbox)

for i in range(3):
    control = TextButton(str(i))
    cbox.append(control)

cbox._children[1].style['height'] = 20


@control.event
def on_mouse_press(*args):
    print "hello"
    print args


@window.event
def on_close():
    pyglet.app.exit()

@window.event
def on_draw():
    window.clear()
    root.render()


from windoh.serialize import dump
open("out.xml", 'w').write( dump(root) )

pyglet.app.run()
